//
//  XiangQingViewController.swift
//  demo
//
//  Created by HAip on 2020/2/20.
//  Copyright © 2020 CodeYu. All rights reserved.
//

import UIKit

class XiangQingViewController: UIViewController {

    // MARK: UI
       
       lazy var nav = HZCustomNavigationBar.customNavigationBar().then { (bar) in
            bar.barBackgroundColor = UIColor(hex: "fbfbfb")
            bar.shadowImageHidden = false
            /* 闭包 相当于block
             { (参数) -> (返回值) in
             
             }
             */
            bar.hz.setItemsToLeft([HZNavigationBarItem.create(normalImage: "back", barItemClickHandler: { (item) in
                self.navigationController?.popViewController(animated: true)
            }), HZNavigationBarItem.create(normalTitle: "详情", barItemClickHandler: {
                (item) in
            })])
        }
    
    lazy var tab = UITableView.init(frame: CGRect.zero, style: .grouped).then{
        $0.separatorStyle = UITableViewCell.SeparatorStyle.none
        $0.delegate = self
        $0.dataSource = self
        $0.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        
    }
    
    var arrowDown = false
        
    override func viewDidLoad() {
        
        super.viewDidLoad()
        view.backgroundColor = UIColor(hex: "f4f4f4")

        view.addSubview(nav)
        
        view.addSubview(tab)
        tab.snp.makeConstraints { (make) in
            make.top.equalTo(nav.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
        
        // 创建数据源
    

        // Do any additional setup after loading the view.
    }
    
    //MARK: -Cell
    
    // 获取 carNumbHeaderCell
    func carNumHeaderView() ->UIView{
        let headView = UIView().then {
            let carNumView = UIView().then{
                
                let numTitleLab = UILabel().then{
                    $0.textColor = UIColor.black
                    $0.font = UIFont.systemFont(ofSize: 15)
                }
                numTitleLab.text = "车牌号码"
                $0.addSubview(numTitleLab)
                numTitleLab.snp.makeConstraints { (make) in
                    make.centerY.equalToSuperview()
                    make.left.equalToSuperview()
                }
                let numStr = "苏A123457"
                var lastLab : UILabel?
                for index in numStr.indices{
                   
                    let lab = UILabel().then {
                        $0.textColor = UIColor.darkGray
                        $0.font = UIFont.boldSystemFont(ofSize: 17)
                        $0.textAlignment = NSTextAlignment.center
                        $0.layer.masksToBounds = true
                        $0.layer.cornerRadius = 5
                        $0.layer.borderWidth = 1.5
                        $0.layer.borderColor = UIColor.gray.cgColor
                        $0.layer.shadowColor = UIColor.darkGray.cgColor
                        $0.layer.shadowOffset = CGSize(width: 1, height: 1)
                        $0.backgroundColor = UIColor(hex: "f4f4f4")
                    }
                   
                    $0.addSubview(lab)
                    print("str:" + "\(numStr[index])")
                    lab.snp.makeConstraints { (make) in
                        make.centerY.equalToSuperview()
                        make.size.equalTo(CGSize(width: 28, height: 38))
                        if(index == numStr.startIndex){
                            make.left.equalTo(numTitleLab.snp.right).offset(12)
                        }else if(index == numStr.index(numStr.startIndex,offsetBy: 2)){
                            make.left.equalTo(lastLab!.snp.right).offset(15)
                        }else if(index == numStr.index(before: numStr.endIndex)){
                            make.left.equalTo(lastLab!.snp.right).offset(4)
                            make.right.equalToSuperview()
                        }else{
                            make.left.equalTo(lastLab!.snp.right).offset(4)
                        }
                    }
                    lab.text = String(numStr[index])
                    lastLab = lab
                }
               
                
                
            }
            $0.addSubview(carNumView)
            carNumView.snp.makeConstraints { (make) in
                make.centerX.equalToSuperview()
                make.top.equalToSuperview().offset(20)
                make.height.equalTo(60)
            }
            
            let carColorLeftLab = UILabel().then({
                $0.textColor = UIColor.black
                $0.font = UIFont.systemFont(ofSize: 16)
            })
            $0.addSubview(carColorLeftLab)
            carColorLeftLab.snp.makeConstraints({ (make) in
                make.left.equalToSuperview().offset(15)
                make.top.equalTo(carNumView.snp.bottom).offset(20)
                make.bottom.equalToSuperview().offset(-20)
            })
            carColorLeftLab.text = "车牌颜色"
            
            
            let carColorRightLab = UILabel().then({
                $0.textColor = UIColor.gray
                $0.font = UIFont.systemFont(ofSize: 14)
            })
            $0.addSubview(carColorRightLab)
            carColorRightLab.snp.makeConstraints({ (make) in
                make.right.equalToSuperview().offset(-15)
                make.centerY.equalTo(carColorLeftLab)
            })
            carColorRightLab.text = "绿色"
        }
        return headView
    }
    
    
    /// 一个标题,一个内容,是否显示下划线
    ///
    /// - Parameters:
    ///   - title: <#title description#>
    ///   - des: <#des description#>
    /// - Returns: <#return value description#>
    func customOneCell(title: String, des: String, bottomLine: Bool) -> UIView{
        let cellView = UIView().then{
            let leftLab = UILabel().then{
                $0.textColor = UIColor.black
                $0.font = UIFont.systemFont(ofSize: 15)
            }
            $0.addSubview(leftLab)
            leftLab.snp.makeConstraints({ (make) in
                make.left.equalToSuperview().offset(15)
                make.height.equalTo(50)
                make.top.bottom.equalToSuperview()
            })
            leftLab.text = title
            
            let rightLab = UILabel().then{
                $0.textColor = UIColor.gray
                $0.font = UIFont.systemFont(ofSize: 14)
            }
            $0.addSubview(rightLab)
            rightLab.snp.makeConstraints({ (make) in
                make.right.equalToSuperview().offset(-15)
                make.centerY.equalTo(leftLab)
            })
            rightLab.text = des
            
            let line = UIView().then{
                $0.backgroundColor = UIColor.lightGray
                $0.isHidden = !bottomLine
            }
            $0.addSubview(line)
            line.snp.makeConstraints({ (make) in
                make.left.right.bottom.equalToSuperview()
                make.height.equalTo(0.5)
            })
        }
        return cellView
    }
    // 两个标题,两个内容
    func customTwoCell(titleOne: String,desOne: String,titleTwo:String,desTwo: String) -> UIView {
        let cellView = UIView().then{
            let titleOneLab = UILabel().then{
                $0.textColor = UIColor.black
                $0.font = UIFont.systemFont(ofSize: 15)
            }
            $0.addSubview(titleOneLab)
            titleOneLab.snp.makeConstraints({ (make) in
                make.left.equalToSuperview().offset(15)
                make.height.equalTo(50)
                make.top.bottom.equalToSuperview()
            })
            titleOneLab.text = titleOne
            
            let desOneLab = UILabel().then{
                $0.textColor = UIColor.gray
                $0.font = UIFont.systemFont(ofSize: 14)
            }
            $0.addSubview(desOneLab)
            desOneLab.snp.makeConstraints({ (make) in
                make.right.equalToSuperview().offset(-UIScreen.main.bounds.size.width/2 - 20)
                make.centerY.equalTo(titleOneLab)
            })
            desOneLab.text = desOne
            
            let titleTwoLab = UILabel().then{
                $0.textColor = UIColor.black
                $0.font = UIFont.systemFont(ofSize: 15)
            }
            $0.addSubview(titleTwoLab)
            titleTwoLab.snp.makeConstraints({ (make) in
                make.left.equalToSuperview().offset(UIScreen.main.bounds.size.width/2 + 15)
                make.top.bottom.equalToSuperview()
            })
            titleTwoLab.text = titleTwo
            
            let desTwoLab = UILabel().then{
                $0.textColor = UIColor.gray
                $0.font = UIFont.systemFont(ofSize: 14)
            }
            $0.addSubview(desTwoLab)
            desTwoLab.snp.makeConstraints({ (make) in
                make.centerY.equalToSuperview()
                make.right.equalToSuperview().offset(-15)
            })
            desTwoLab.text = desTwo
        }
        return cellView
    }
    
    // 审批
    func customThreeCell(indexPath: IndexPath) -> UIView {
        let cellView = UIView().then{
            let statueImg = UIImageView()
            $0.addSubview(statueImg)
            statueImg.snp.makeConstraints({ (make) in
                make.left.equalToSuperview().offset(25)
                make.top.equalToSuperview().offset(18)
            })
            statueImg.image = UIImage(named: "biaozhi")
            if indexPath.row != 0{
                let topLine = UIView().then{
                    $0.backgroundColor = UIColor.gray
                }
                $0.addSubview(topLine)
                topLine.snp.makeConstraints({ (make) in
                    make.top.equalToSuperview()
                    make.centerX.equalTo(statueImg)
                    make.bottom.equalTo(statueImg.snp.top)
                    make.width.equalTo(0.5)
                })
            }
            
            // 不等于最后一个
            if indexPath.row != 1{
                let bottomLine = UIView().then{
                    $0.backgroundColor = UIColor.gray
                }
                $0.addSubview(bottomLine)
                bottomLine.snp.makeConstraints({ (make) in
                    make.bottom.equalToSuperview()
                    make.centerX.equalTo(statueImg)
                    make.top.equalTo(statueImg.snp.bottom)
                    make.width.equalTo(0.5)
                })
            }
            
            let titleLab = UILabel().then{
                $0.textColor = UIColor.gray
                $0.font = UIFont.systemFont(ofSize: 16)
            }
            $0.addSubview(titleLab)
            titleLab.snp.makeConstraints({ (make) in
                make.centerY.equalTo(statueImg)
                make.left.equalTo(statueImg.snp.right).offset(15)
            })
            
            let timeLab = UILabel().then{
                $0.textColor = UIColor.blue
                $0.font = UIFont.systemFont(ofSize: 13)
            }
            $0.addSubview(timeLab)
            timeLab.snp.makeConstraints({ (make) in
                make.left.equalTo(titleLab)
                make.top.equalTo(titleLab.snp.bottom).offset(15)
                make.bottom.equalToSuperview().offset(-18)
            })
            
            if indexPath.row == 0{
                titleLab.text = "申请完成"
                timeLab.text = "申请时间: 2030 11.33.33"
            }else{
                titleLab.text = "发起申请"
                timeLab.text = "申请时间: 2022 11.33.33"
            }
            
        }
        
        
        return cellView
    }

   

}

extension XiangQingViewController: UITableViewDataSource,UITableViewDelegate
{
   
    func numberOfSections(in tableView: UITableView) -> Int {
        return 4
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch section {
        case 0:
            return 1
        case 1:
            return 8
        case 2:
            if arrowDown {
                return 15
            }else{
                return 2
            }
        case 3:
            return 2
        default:
            return 0
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView .dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.selectionStyle = UITableViewCell.SelectionStyle.none
        for subView in cell.subviews {
            subView.removeFromSuperview()
        }
        switch indexPath.section {
        case 0:
        
            let subView = self.carNumHeaderView()
            cell.addSubview(subView)
            subView.snp.makeConstraints { (make) in
               make.edges.equalToSuperview()
            }
        
            break
        case 1:
                    var subView = UIView()
                    switch indexPath.row{
                    case 0: subView = self.customOneCell(title: "客户姓名", des: "中数据科技", bottomLine: true)
                        break
                    case 1: subView = self.customOneCell(title: "证件类型", des: "营业执照", bottomLine: true)
                        break
                    case 2: subView = self.customOneCell(title: "证件号码", des: "223434", bottomLine: true)
                        break
                    case 3: subView = self.customOneCell(title: "银行名称", des: "广发银行", bottomLine: true)
                        break
                    case 4: subView = self.customOneCell(title: "银行卡号", des: "311231**321", bottomLine: true)
                        break
                    case 5: subView = self.customOneCell(title: "手机号码", des: "318043242", bottomLine: true)
                        break
                    case 6: subView = self.customOneCell(title: "发卡中心", des: "河南省郑州市", bottomLine: true)
                        break
                    case 7: subView = self.customOneCell(title: "发卡状态", des: "已发行", bottomLine: true)
                        break
                    default: break
                    }
                    cell.addSubview(subView)
                    subView.snp.makeConstraints { (make) in
                        make.edges.equalToSuperview()
                    }
            
            break
        case 2:
            var subView = UIView()
            switch indexPath.row{
            case 0:subView = self.customOneCell(title: "车辆所有人", des: "KFSDF", bottomLine: false)
                break
            case 1:subView = self.customOneCell(title: "发证日期", des: "131-123-123", bottomLine: false)
                break
            case 2:subView = self.customOneCell(title: "注册日期", des: "3442-234-342", bottomLine: false)
                break
            case 3:subView = self.customOneCell(title: "使用性质", des: "营运客车", bottomLine: false)
                break
            case 4:subView = self.customOneCell(title: "车辆类型", des: "车车车", bottomLine: false)
                break
            case 5:subView = self.customOneCell(title: "品牌类型", des: "宝马", bottomLine: false)
                break
            case 6:subView = self.customOneCell(title: "外扩尺寸 长/宽/高", des: "333/111/444", bottomLine: false)
                break
            case 7:subView = self.customTwoCell(titleOne: "总质量(kg)", desOne: "1450", titleTwo: "整体质量(kg)", desTwo: "1313")
                break
            case 8:subView = self.customTwoCell(titleOne: "核定在质量(kg)", desOne: "750", titleTwo: "总牵引", desTwo: "342")
                break
            case 9:subView = self.customTwoCell(titleOne: "核定人数", desOne: "5", titleTwo: "车辆发动机号", desTwo: "EA11")
                break
            case 10:subView = self.customTwoCell(titleOne: "车轴数", desOne: "2", titleTwo: "车距", desTwo: "20mm")
                break
            case 11:subView = self.customOneCell(title:"车辆识别代码", des: "dfsodfs", bottomLine: false)
                break
            case 12:subView = self.customOneCell(title: "行驶证品牌型号", des: "第三方", bottomLine: false)
                break
            case 13:subView = self.customOneCell(title: "检验记录", des: "良", bottomLine: false)
                break
            case 14:subView = self.customOneCell(title: "档案编号", des: "sdfa", bottomLine: false)
                break
            default:break
            }
            cell.addSubview(subView)
            subView.snp.makeConstraints { (make) in
                make.edges.equalToSuperview()
            }
        case 3:
            let subView = self.customThreeCell(indexPath: indexPath)
            cell.addSubview(subView)
            subView.snp.makeConstraints { (make) in
                make.edges.equalToSuperview()
            }
            break
        default: break

            
        }
       
        return cell
        
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        if section == 2 || section == 3 {
            return 50
        }else{
            return 0
        }
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if section == 2 || section == 3 {
            let headerView = UIView().then{
                $0.backgroundColor = UIColor.white
                let leftLab = UILabel().then{
                    $0.textColor = UIColor.black
                    $0.font = UIFont.boldSystemFont(ofSize: 18)
                }
                $0.addSubview(leftLab)
                leftLab.snp.makeConstraints({ (make) in
                    make.left.equalToSuperview().offset(15)
                    make.top.bottom.equalToSuperview()
                })
                
                
                if section == 2{
                    leftLab.text = "车辆信息"
                }else{
                    leftLab.text = "审批记录"
                }
                
                let line = UIView().then{
                    $0.backgroundColor = UIColor.lightGray
                }
                $0.addSubview(line)
                line.snp.makeConstraints({ (make) in
                    make.left.right.bottom.equalToSuperview()
                    make.height.equalTo(0.5)
                })
               
            }
            return headerView
        
        }else{
            return nil
        }
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        if section == 2 {
            return 30 + 15
        }else if section == 3{
            return 40 + 45
        }else{
            return 15
        }
    }
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        if section == 2 {
            let footerView = UIView().then{
               let arrowBtn = UIButton(type: .custom)
                arrowBtn.setImage(UIImage(named: "down"), for: .normal)
                arrowBtn.setImage(UIImage(named: "up"), for: .selected)
                arrowBtn.isSelected = arrowDown
                arrowBtn.backgroundColor = UIColor.white
                $0.addSubview(arrowBtn)
                arrowBtn.snp.makeConstraints({ (make) in
                    make.left.right.top.equalToSuperview()
                    make.height.equalTo(30)
                })
                arrowBtn.actionBlock = { (sender) in
                    self.arrowDown = !sender.isSelected
                    self.tab.reloadData()
                }
            }
            
            
            return footerView
        }else if section == 3{
            let footerView = UIView().then{
                let btn = UIButton(type: .custom)
                btn.setTitle("物流信息", for: .normal)
                btn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
                btn.backgroundColor = UIColor.red
                btn.layer.masksToBounds = true
                btn.layer.cornerRadius = 3
                $0.addSubview(btn)
                btn.snp.makeConstraints({ (make) in
                   make.left.equalToSuperview().offset(15)
                   make.right.equalToSuperview().offset(-15)
                    make.bottom.equalToSuperview()
                    make.height.equalTo(45)
                })
                btn.actionBlock = { _ in
                    
                }
            }
            
            return footerView
        }else{
            return nil
        }
    }
}
